﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using TeachMe.Application.Configuration;
using TeachMe.Application.Properties;

namespace TeachMe.Application.Forms
{
    public partial class OnlineDictsForm : PseudoModalForm
    {
        public class OnlineDicts
        {
            public class Item
            {
                public int Id { get; set; }
                public string Name { get; set; }
                public string Description { get; set; }
                public string From { get; set; }
                public string To { get; set; }
                public string Author { get; set; }
                public string Path { get; set; }
                public string Downloads { get; set; }
                public string Entries { get; set; }
            }

            public List<Item> Items { get; set; }
        }

        private readonly Root _root;
        private OnlineDicts _onlineDicts;

        public OnlineDictsForm(Root root)
        {
            _root = root;

            InitializeComponent();

            WindowProportions.Lock(this, _root.MachineSettings);
            ListViewColumnsProportions.Lock(this, lvDicts, _root.MachineSettings);
        }

        public List<OnlineDicts.Item> Result
        {
            get
            {
                List<OnlineDicts.Item> result = new List<OnlineDicts.Item>();

                if (lvDicts.SelectedObjects != null)
                {
                    foreach (OnlineDicts.Item item in lvDicts.SelectedObjects)
                    {
                        result.Add(item);
                    }
                }

                return result;
            }
        }

        public void Display(WebContentDownloader<OnlineDicts, object>.Job[] jobs)
        {
            _onlineDicts = jobs[0].Result;

            // temporary language fix
            foreach (OnlineDicts.Item item in _onlineDicts.Items)
            {
                if (Languages.GetByCode(item.From) != null)
                {
                    item.From = Languages.GetByCode(item.From).Localized;
                }
                if (Languages.GetByCode(item.To) != null)
                {
                    item.To = Languages.GetByCode(item.To).Localized;
                }
            }

            lvDicts.SetObjects(_onlineDicts.Items);
        }

        private void OnShown(object sender, EventArgs e)
        {
            Reload();
        }

        private void Reload()
        {
            List<WebContentDownloader<OnlineDicts, object>.Job> jobs = new List<WebContentDownloader<OnlineDicts, object>.Job>();
            jobs.Add(new WebContentDownloader<OnlineDicts, object>.Job{Link = "http://www.teachme.cz/dicts.php"});

            WebContentDownloader<OnlineDicts, object>.Download(_root, this, jobs.ToArray(), Resources.CheckingOnlineDicts, Display);
        }

        private void OnClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            //
        }

        private void OnSelectionChanged(object sender, EventArgs e)
        {
            bOK.Enabled = lvDicts.SelectedObjects != null && lvDicts.SelectedObjects.Count > 0;
        }

        private void OnRefreshClick(object sender, EventArgs e)
        {
            Reload();
        }

        private void OnSelectAllClick(object sender, EventArgs e)
        {
            lvDicts.SelectAll();
        }

        private void OnDictsCellClick(object sender, BrightIdeasSoftware.CellClickEventArgs e)
        {
            if (bOK.Enabled && e.ClickCount >= 2)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void OnDownloadClick(object sender, EventArgs e)
        {
            if (lvDicts.SelectedObjects != null && lvDicts.SelectedObjects.Count > 0)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
        }

        private void OnMenuOpening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            miDownload.Enabled = lvDicts.SelectedObjects != null && lvDicts.SelectedObjects.Count > 0;
        }
    }
}
